package ca.polymtl.liom.gui;

import javax.swing.JPanel;
import java.awt.BorderLayout;

import javax.swing.JList;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.factories.FormFactory;
import javax.swing.JLabel;
import javax.swing.JFormattedTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JScrollPane;
import ca.polymtl.liom.gui.LIOMStimulationListModel;
import ca.polymtl.liom.stim.LIOMStimulation;

import java.awt.Dimension;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;

public class LIOMStimulationListDisplay extends JPanel {
	private LIOMStimulationListModel p_model= null;
	private JFormattedTextField frequencyTextField;
	private JFormattedTextField trainSpacingTextField;
	private JFormattedTextField trainLengthTextField;
	private JFormattedTextField jitterTextField;
	private JFormattedTextField pulseDurationTextField;
	private JList p_list;
	/**
	 * Create the panel.
	 */
	public LIOMStimulationListDisplay() {
		setLayout(new BorderLayout(5, 5));
		
		JPanel stim_data_panel = new JPanel();
		add(stim_data_panel,BorderLayout.CENTER);
		stim_data_panel.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),},
			new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,}));
		
		JLabel lblTrainLength = new JLabel("Train length:");
		stim_data_panel.add(lblTrainLength, "2, 2, right, default");
		
		trainLengthTextField = new JFormattedTextField();
		stim_data_panel.add(trainLengthTextField, "4, 2, fill, default");
		
		JLabel lblFrequency = new JLabel("Frequency");
		stim_data_panel.add(lblFrequency, "2, 4, right, default");
		
		frequencyTextField = new JFormattedTextField();
		stim_data_panel.add(frequencyTextField, "4, 4, fill, default");
		
		JLabel lblTrainSpacing = new JLabel("Train Spacing");
		stim_data_panel.add(lblTrainSpacing, "2, 6, right, default");
		
		trainSpacingTextField = new JFormattedTextField();
		stim_data_panel.add(trainSpacingTextField, "4, 6, fill, default");
		
		JLabel lblPulseDuration = new JLabel("Pulse Duration");
		stim_data_panel.add(lblPulseDuration, "2, 8, right, default");
		
		pulseDurationTextField = new JFormattedTextField();
		stim_data_panel.add(pulseDurationTextField, "4, 8, fill, default");
		
		JLabel lblJitter = new JLabel("Jitter");
		stim_data_panel.add(lblJitter, "2, 10, right, default");
		
		jitterTextField = new JFormattedTextField();
		stim_data_panel.add(jitterTextField, "4, 10, fill, default");
		
		JPanel button_panel = new JPanel();
		add(button_panel, BorderLayout.SOUTH);
		
		JButton btnAddStimulation = new JButton("Add Stimulation");
		btnAddStimulation.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				// Read each text field and populate a new LIOMStimulation
				// Make sure all data is ok
				float train_length = Float.parseFloat(getTrainLengthTextField().getText());
				float train_spacing = Float.parseFloat(getTrainSpacingTextField().getText());
				float frequency = Float.parseFloat(getFrequencyTextField().getText());
				float pulse_duration = Float.parseFloat(getPulseDurationTextField().getText());
				float jitter = Float.parseFloat(getJitterTextField().getText());
				if( train_length >= 0 && 
						train_spacing >= 0 && 
						frequency >= 0 && 
						pulse_duration > 0.1 &&
						jitter >= 0 ){
					// Then ask for a new name
					//JTextDialog dlg = new JTextDialog();
					LIOMStimulation new_stim = new LIOMStimulation();
					new_stim.train_length = train_length;
					new_stim.train_spacing = train_spacing;
					new_stim.frequency = frequency;
					new_stim.pulse_duration =  pulse_duration;
					new_stim.jitter = jitter;
					// Then add to the list
					p_model.addElement(new_stim);
				}
				else
				{
					// Warning dialog that parameters are not ok
				}
				
			}
		});
		button_panel.add(btnAddStimulation);
		
		JButton btnDeleteStimulation = new JButton("Delete Stimulation");
		btnDeleteStimulation.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int remove_index = getList().getSelectedIndex();
				p_model.removeElement(remove_index);
			}
		});
		button_panel.add(btnDeleteStimulation);
		
		JButton btnSelect = new JButton("Select");
		btnSelect.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				// Need to emit a signal that the current stimulation has changed.
			}
		});
		button_panel.add(btnSelect);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setMinimumSize(new Dimension(150, 23));
		add(scrollPane, BorderLayout.WEST);
		
		p_list = new JList();
		p_list.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent arg0) {
				// Change the text edit values according to list
			}
		});
		p_list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		p_list.setPreferredSize(new Dimension(150, 0));
		p_list.setMinimumSize(new Dimension(150, 0));
		p_model=new LIOMStimulationListModel();
		p_list.setModel(p_model);
		scrollPane.add(p_list);
	}

	protected JFormattedTextField getFrequencyTextField() {
		return frequencyTextField;
	}
	protected JFormattedTextField getTrainSpacingTextField() {
		return trainSpacingTextField;
	}
	protected JFormattedTextField getTrainLengthTextField() {
		return trainLengthTextField;
	}
	protected JFormattedTextField getJitterTextField() {
		return jitterTextField;
	}
	protected JFormattedTextField getPulseDurationTextField() {
		return pulseDurationTextField;
	}
	protected JList getList() {
		return p_list;
	}
}
